/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.data.collections.Map;

import fever.commands.JSCommand;
import fever.commands.JSMethodName;
import fever.log.FeverDebug;

import flash.external.ExternalInterface;

/**
 * Registration repository for {@link fever.commands.JSCommand}
 *
 * @author Romain Ecarnot
 */
class fever.commands.JSLocator
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _map : Map = new Map();
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Registers a new {@link fever.commands.JSCommand} using {@code methodName} 
	 * function name and {@code fCode} javascript source code.
	 * 
	 * <p>A {@link fever.commands.JSCommand} is automatically created.
	 * 
	 * @return {@code true} if registration success.
	 */
	public static function register( methodName : JSMethodName, fCode : String  ) : Boolean
	{
		var command : JSCommand = new JSCommand( methodName, fCode );
		return ( getCommand( methodName ) != undefined );
	}
	
	/**
	 * Registers the {@code command} using {@code methodName} function name.
	 * 
	 * @return {@code true} if registration success.
	 */
	public static function push( methodName : JSMethodName, command : JSCommand  ) : Boolean
	{
		if( ExternalInterface.available ) return _register( methodName, command );
		else
		{ 
			FeverDebug.ERROR( "JS." + methodName + "() is not registred" );
			return false;
		}
	} 	
	
	/**
	 * Returns {@link fever.commands.JSCommand} associated with passed-in 
	 * {@code methodName} function name.
	 * 
	 * @return {@link fever.commands.JSCommand} or {@code null} if {@code methodName} 
	 * is not registred.
	 */
	public static function getCommand( methodName : JSMethodName ) : JSCommand
	{
		if( _map.containsKey( methodName ) ) return _map.get( methodName );
		else
		{
			FeverDebug.ERROR( "JS." + methodName + "() is not registred" );
			return null;
		}
	}
	
	/**
	 * Removes {@link fever.commands.JSCommand} registred under 
	 * {@code methodName} method name.
	 * 
	 * @return {@code true} if command is successfully removed.
	 */
	public static function remove( methodName : JSMethodName ) : Boolean
	{
		if( _map.containsKey( methodName ) )
		{
			_map.remove( methodName );
			return true;
		}
		return false;
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private static function _register( methodName : JSMethodName, command : JSCommand ) : Boolean
	{
		if( ! _map.containsKey( methodName ) )
		{
			_map.put( methodName, command );
			return true;
		}
		else return false;
	}
	
	/** Constructor. */
	private function JSLocator() {}	
}